<template>
    <div class="addBrand-container">
      <el-page-header @back="goBack" :content="content" style="margin-bottom:20px">
      </el-page-header>
    <div class="container">
      <el-form ref="ruleForm"
               :model="ruleForm"
               :inline="false"
               label-width="180px"
               class="demo-ruleForm">
        <el-form-item label="图片:"
                      prop="avatar">
          <uploadImage class="uploadImage" :imageUrl="ruleForm.image" @imageUrl2="imageUrl2" />
        </el-form-item>
        <el-form-item label="商品名称:"
                      prop="name">
          <el-input v-model="ruleForm.name"
                    placeholder="请输入商品名称"
                    maxlength="12" />
        </el-form-item>
        <el-form-item label="保质期:"
                      prop="expiration">
          <el-input v-model.number="ruleForm.expirationDate"
                    placeholder="请输入保质期"
                    maxlength="11" >
          <template slot="append">天</template>
          </el-input>
        </el-form-item>
        <el-form-item label="价格:"
                      prop="price"
                      class="idNumber">
          <el-input v-model="ruleForm.price"
                    placeholder="请输入商品价格"
                    maxlength="18" >
                    <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="ruleForm.categoryId" placeholder="类型" >
            <el-option :label="category.name" :value="category.id" v-for="(category,index) in categorys" :key="index"></el-option>
            <!-- <el-option label="区域二" value="beijing"></el-option> -->
          </el-select>
        </el-form-item>
        <el-form-item label="商品介绍:"
                      prop="description">
          <el-input v-model="ruleForm.description"
                    placeholder="请输入商品介绍"
                    type="textarea"
                    :autosize="{ minRows: 2, maxRows: 4}"
                    style="width: 295px;" />
        </el-form-item>
        <div class="subBox address">
          <!-- <el-form-item> -->
          <el-button @click="() => $router.push('/home/product')">
            取消
          </el-button>
          <el-button type="primary"
                     :class="{ continue: actionType === 'add' }"
                     @click="submitForm(actionType)">
            保存
          </el-button>
          <!-- <el-button v-if="actionType == 'add'"
                     type="primary"
                     @click="submitForm('ruleForm', true)">
            保存并继续添加
          </el-button> -->
          <!-- </el-form-item> -->
        </div>
      </el-form>
    </div>
  </div>
</template>


<script>
import uploadImage from '@/components/upload/uploadImage.vue'
import {queryProductById,updateProduct,addProduct} from '@/api/product'
import {categoryPage} from '@/api/category'
export default{
  components:{
    uploadImage
  },
  data() {
    return {
      content:'添加员工',
      ruleForm:{
        categoryId: 0,
        description: "",
        expirationDate: 0,
        id: 0,
        image: "",
        name: "",
        price: 0,
        status:0
      },
      rules:'',
      actionType:'',
      categorys:[],
      user:null
    }
  },
  created(){
    this.actionType = this.$route.query.methods
    if(this.actionType == 'add'){
      this.content='添加商品'
    }else{
      this.content='修改商品信息'
      queryProductById(parseInt(this.$route.query.id)).then((res)=>{
        // this.ruleForm =  Object.assign(res.data)
        Object.keys(this.ruleForm).forEach(key => {
          this.ruleForm[key] = res.data[key] == undefined ? this.ruleForm[key] : res.data[key]
      })

      }).catch((err)=>{
        this.$message.error('queryProductById:'+err)
      })
      
    }
    //查询所有分类
    const res2 = categoryPage('',1,10,'')
    res2.then((res)=>{
      // console.log(res.data)
      this.categorys = res.data.records
      // let category = {
      //   name:'',
      //   id:0
      // }
      // this.categorys
    }).catch((err)=>{
      this.$message.error(err)
    })
    
  },
  methods:{
    submitForm(value){
      console.log(this.ruleForm)
      let res;
      if(value == 'add'){
        res = addProduct(this.ruleForm)
        res.then((res)=>{
          this.$message({
            message: '添加成功',
            type: 'success'
          });
          this.$router.push('/home/product')
        }).catch((err)=>{
          this.$message.error(err);
        })
      }
      else if(value == 'update'){
        console.log(value)
        console.log(this.ruleForm)
        res = updateProduct(this.ruleForm)
        res.then((res)=>{
          this.$message({
            message: '更新成功',
            type: 'success'
          });
          console.log(res)
          this.$router.push('/home/product')
        }).catch((err)=>{
          console.log(err)
          this.$message.error(err);
        })
      }
    },
    goBack(){
      this.$router.go(-1)
    },
    imageUrl2(imageUrl2){
          console.log('imageUrl2:'+imageUrl2)
          this.ruleForm.image = imageUrl2
    }
  }
}
</script>

<style lang="scss" scoped>
.addBrand {
  &-container {
    margin: 30px;
    margin-top: 0px;
    .HeadLable {
      background-color: transparent;
      margin-bottom: 0px;
      padding-left: 0px;
    }
    .container {
      position: relative;
      z-index: 1;
      background: #fff;
      padding: 30px;
      border-radius: 4px;

    }
    .idNumber {
      margin-bottom: 39px;
    }

    .el-form-item {
      margin-bottom: 29px;
    }
    .el-input {
      width: 293px;
    }
  }
}

.demo-ruleForm{
  width: 600px;
}

.uploadImage{
  border-radius: 5px;
}
</style>